package Test;

import PolymorphismLearning.Animal;
import Animal.*;

public class testPolymorphism {
    public static void main(String[] args) {
        /**
         * 正常定义
         */
        System.out.println("===正常定义↓===");
        Cat cat = new Cat("小黑",12,4);
        System.out.println(cat.getWeight()+","+ cat.getName());
        cat.eat();

        /**
         * 向上转型
         */
        System.out.println("===继承↓===");
        Animal catOne = new Cat("花花",6.3,5);
        System.out.println(catOne.getName());
        catOne.eat();

        /**
         * 向下转型只能强转
         */
        System.out.println("===向下转型↓===");
        Animal animalOne = new Cat("小白",8,7);
        Cat animalOneCat = (Cat)animalOne;
        System.out.println(animalOneCat.getWeight()+" "+animalOneCat.getName()+" "+animalOne.getName());

        System.out.println("===向下转型一般要加判断语句↓===");
        Animal catThree = new Cat(); //先向上转型
        if (catThree instanceof Cat){
            Cat temp = (Cat) catThree;
            System.out.println("Cat转型成功");
        } else if (catThree instanceof Dog){
            Dog temp = (Dog) catThree;
            System.out.println("Dog转型成功");
        }
        Animal dogOne = new Dog();
        if (dogOne instanceof Cat){
            Cat temp = (Cat) dogOne;
            System.out.println("Cat转型成功");
        } else if (dogOne instanceof Dog){
            Dog temp = (Dog) dogOne;
            System.out.println("Dog转型成功");
        }

    }
}
